package com.ilongsay.utils;

import com.ilongsay.entity.ChaiFenSetting;
import com.ilongsay.entity.FaPiao;
import com.ilongsay.entity.FaPiaoDetail;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @Author ilongsay
 * @Email ilongsay@163.com
 * @Copyright 版本所有，侵权必究！
 * @Datetime 2019-01-01 21:41
 * @Describution 确定主表和明细表的含税金额和未税金额，方便后期的计算
 */
public class ConfirmUtils {

    /**
     * @param list 多张发票的集合
     * @return void
     * 校验多张发票
     * TODO:有点问题，这里的数据需要重新进行计算，并不完美，相同的代码太多，需要重构
     */
    public static void confirmAmount(List<FaPiao> list) {
        for (FaPiao faPiao : list) {
            String total = "0";//含税金额
            String untotal = "0";//未税金额
            String taxAmount = "0";//税额
            List<FaPiaoDetail> faPiaoDetails = faPiao.getFaPiaoDetails();
            //遍历明细信息，求出汇总的金额；
            for (FaPiaoDetail faPiaoDetail : faPiaoDetails) {
                //判断是否含税，1为含税金额
                if (faPiaoDetail.getTaxFlag().equals("1")) {
                    //计算出汇总的含税金额，对原始数据保留2位小数
                    total = CalcUtils.add(CalcUtils.sround(faPiaoDetail.getTotal(), 2), total);

                    //那么此时的未税金额是： 含税金额÷(1+税率)保留2位小数
                    String untotal1 = CalcUtils.div(faPiaoDetail.getTotal(),
                            CalcUtils.add("1", faPiaoDetail.getTaxRate()), 2);
                    untotal = CalcUtils.add(untotal1, untotal);//未税金额汇总
                    faPiaoDetail.setUntotal(untotal1);//设置未税金额的值

                    //此时的税额计算是：含税金额÷(1+税率) * 税率：保留2位小数
                    String tax = CalcUtils.sround(
                            CalcUtils.mul(
                                    CalcUtils.div(faPiaoDetail.getTotal(),
                                            CalcUtils.add("1", faPiaoDetail.getTaxRate()), 10),
                                    faPiaoDetail.getTaxRate()), 2);
                    //税额汇总
                    taxAmount = CalcUtils.add(tax, taxAmount);
                    faPiaoDetail.setTaxAmount(tax);//明细设置税额

                } else if (faPiaoDetail.getTaxFlag().equals("0")) {
                    //未税金额汇总
                    untotal = CalcUtils.add(
                            CalcUtils.sround(faPiaoDetail.getTotal(), 2), untotal);
                    //设置明细的未税金额
                    faPiaoDetail.setUntotal(CalcUtils.sround(faPiaoDetail.getTotal(), 2));

                    //含税金额 = 未税金额*（1+税率）,保留2位小数
                    total = CalcUtils.add(
                            CalcUtils.sround(
                                    CalcUtils.mul(faPiaoDetail.getUntotal(),
                                            CalcUtils.add("1", faPiaoDetail.getTaxRate())), 2), total);
                    //设置明细的含税金额
                    faPiaoDetail.setTotal(CalcUtils.sround(
                            CalcUtils.mul(faPiaoDetail.getUntotal(),
                                    CalcUtils.add("1", faPiaoDetail.getTaxRate())), 2));
                    //明细的税额
                    String tax = CalcUtils.sround(
                            CalcUtils.mul(faPiaoDetail.getUntotal(), faPiaoDetail.getTaxRate()), 2);
                    //税额 = 未税金额 * 税率，保留2位小数
                    taxAmount = CalcUtils.add(tax, taxAmount);
                    faPiaoDetail.setTaxAmount(tax);//设置明细税额
                }
            }
            faPiao.setTotal(total);
            faPiao.setUntotal(untotal);
            faPiao.setTaxAmount(taxAmount);
        }
    }

    /**
     * @param faPiao 主表订单明细实体
     * @return void
     * 数据校验功能，原始数据导入到服务内，进行校验,校验单张发票
     */
    public static void confirmAmount(FaPiao faPiao) {
        String total = "0";//含税金额
        String untotal = "0";//未税金额
        String taxAmount = "0";//税额
        List<FaPiaoDetail> faPiaoDetails = faPiao.getFaPiaoDetails();
        //遍历明细信息，求出汇总的金额；
        for (FaPiaoDetail faPiaoDetail : faPiaoDetails) {
            //判断是否含税，1为含税金额
            if (faPiaoDetail.getTaxFlag().equals("1")) {
                //计算出汇总的含税金额，对原始数据保留2位小数
                total = CalcUtils.add(CalcUtils.sround(faPiaoDetail.getTotal(), 2), total);

                //那么此时的未税金额是： 含税金额÷(1+税率)保留2位小数
                untotal = CalcUtils.add(CalcUtils.div(faPiaoDetail.getTotal(),
                        CalcUtils.add("1", faPiaoDetail.getTaxRate()), 2), untotal);
                faPiaoDetail.setUntotal(
                        CalcUtils.div(faPiaoDetail.getTotal(),
                                CalcUtils.add("1", faPiaoDetail.getTaxRate()), 2));

                //明细的税额
                String tax = CalcUtils.sround(
                        CalcUtils.mul(
                                CalcUtils.div(faPiaoDetail.getTotal(),
                                        CalcUtils.add("1", faPiaoDetail.getTaxRate()), 10),
                                faPiaoDetail.getTaxRate()), 2);
                //此时的税额计算是：含税金额÷(1+税率) * 税率：保留2位小数，判断原始数据表是否有税额
                taxAmount = CalcUtils.add(tax, taxAmount);
                faPiaoDetail.setTaxAmount(tax);//明细设置税额

            } else if (faPiaoDetail.getTaxFlag().equals("0")) {
                //未税金额汇总
                untotal = CalcUtils.add(
                        CalcUtils.sround(faPiaoDetail.getTotal(), 2), untotal);
                //设置明细的未税金额
                faPiaoDetail.setUntotal(CalcUtils.sround(faPiaoDetail.getTotal(), 2));

                //含税金额 = 未税金额*（1+税率）,保留2位小数
                total = CalcUtils.add(
                        CalcUtils.sround(
                                CalcUtils.mul(faPiaoDetail.getUntotal(),
                                        CalcUtils.add("1", faPiaoDetail.getTaxRate())), 2), total);
                //设置明细的含税金额
                faPiaoDetail.setTotal(CalcUtils.sround(
                        CalcUtils.mul(faPiaoDetail.getUntotal(),
                                CalcUtils.add("1", faPiaoDetail.getTaxRate())), 2));
                //明细的税额
                String tax = CalcUtils.sround(
                        CalcUtils.mul(faPiaoDetail.getUntotal(), faPiaoDetail.getTaxRate()), 2);
                //税额 = 未税金额 * 税率，保留2位小数
                taxAmount = CalcUtils.add(tax, taxAmount);
                faPiaoDetail.setTaxAmount(tax);//设置明细税额
            }
        }
        faPiao.setTotal(total);
        faPiao.setUntotal(untotal);
        faPiao.setTaxAmount(taxAmount);
    }

    /**
     * @param faPiao         主表订单
     * @param chaiFenSetting 拆分设置
     * @return void
     * 拆分后进行的数据校验
     */
    public static void confirmAmount(FaPiao faPiao, ChaiFenSetting chaiFenSetting) {
        String total = "0";//含税金额
        String untotal = "0";//未税金额
        String taxAmount = "0";//税额
        List<FaPiaoDetail> faPiaoDetails = faPiao.getFaPiaoDetails();
        //遍历明细信息，求出汇总的金额；
        for (FaPiaoDetail faPiaoDetail : faPiaoDetails) {
            //判断是否含税，1为含税金额
            if (chaiFenSetting.getIsTax().equals("1")) {
                //设置金额保留2位小数
                faPiaoDetail.setTotal(CalcUtils.sround(faPiaoDetail.getTotal(), 2));

                //计算出汇总的含税金额，对原始数据保留2位小数
                total = CalcUtils.add(CalcUtils.sround(faPiaoDetail.getTotal(), 2), total);

                untotal = CalcUtils.sround(CalcUtils.add(faPiaoDetail.getUntotal(), untotal),2);//汇总未税金额

                taxAmount = CalcUtils.sround(CalcUtils.add(faPiaoDetail.getTaxAmount(), taxAmount),2);//汇总税额

            } else if (chaiFenSetting.getIsTax().equals("0")) {
                //未税金额汇总
                untotal = CalcUtils.add(
                        CalcUtils.sround(faPiaoDetail.getUntotal(), 2), untotal);

                //含税金额汇总 = 各个明细的含税金额汇总
                total = CalcUtils.sround(CalcUtils.add(faPiaoDetail.getTotal(), total),2);
                //设置金额保留2位小数
                faPiaoDetail.setTotal(CalcUtils.sround(faPiaoDetail.getTotal(), 2));
                //税额也是叠加
                taxAmount = CalcUtils.add(CalcUtils.sround(faPiaoDetail.getTaxAmount(),2), taxAmount);

            }
        }
        faPiao.setTotal(total);
        faPiao.setUntotal(untotal);
        faPiao.setTaxAmount(taxAmount);
    }

    /**
     * @param faPiao
     * @return void
     * 对已有的数据进行修正
     */
    public static void fixAmount(FaPiao faPiao) {
        String total = "0";//含税金额
        String untotal = "0";//未税金额
        String taxAmount = "0";//税额
        List<FaPiaoDetail> faPiaoDetails = faPiao.getFaPiaoDetails();
        //遍历明细信息，求出汇总的金额；
        for (FaPiaoDetail faPiaoDetail : faPiaoDetails) {
            //未税金额
            untotal = CalcUtils.add(faPiaoDetail.getUntotal(),untotal);

            //明细的税额
            taxAmount = CalcUtils.add(faPiaoDetail.getTaxAmount(), taxAmount);

            //计算出汇总的含税金额，按照这种方式目前有些小的bug，暂时不采取未税+税额的方式来进行
            //total = CalcUtils.add(untotal,taxAmount);
            total = CalcUtils.add(faPiaoDetail.getTotal(), total);
        }
        faPiao.setTotal(total);
        faPiao.setUntotal(untotal);
        faPiao.setTaxAmount(taxAmount);

    }


    /**
     * @param faPiaos
	 * @param isNote 为true时代表需要记录明细单号，为false时代表不需要
     * @return void
     * 这里是将明细的备注汇总到主表的备注中！TODO:日后需要优化
     */
    public static void mergeNodes(List<FaPiao> faPiaos,Boolean isNote) {
        //遍历发票信息
        for (FaPiao faPiao : faPiaos) {
            Set<String> notes = new TreeSet<>();
            Set<String> billCodes = new TreeSet<>();
            //遍历明细信息
            for (FaPiaoDetail faPiaoDetail : faPiao.getFaPiaoDetails()) {
                if (faPiaoDetail.getNotes() != null && isNote == false) {
                    notes.add(faPiaoDetail.getNotes());//将遍历的明细的信息保存到set中进行去重
                }else if (faPiaoDetail.getNotes() != null && isNote == true) {
                    notes.add(faPiaoDetail.getNotes());
                    billCodes.add(faPiaoDetail.getOldCode());//将明细的订单号加入
                }
            }

            String[] note =  notes.toString().replace(" ", "").replace("[", "").
                    replace("]", "").split(",");

            String[] billCode =  billCodes.toString().replace(" ", "").replace("[", "").
                    replace("]", "").split(",");

            Set<String> strings = new TreeSet<>();
            for (String no : note) {
                strings.add(no);
            }

            if (isNote == true) {
                Set<String> bill = new TreeSet<>();
                for (String s : billCode) {
                    bill.add(s);
                }
                faPiao.setNote(strings.toString().
                        replace("[", "").
                        replace("]", "") +","+
                               bill.toString().
                        replace("[", "").
                        replace("]", ""));
            }else {
                faPiao.setNote(strings.toString().
                        replace("[", "").
                        replace("]", ""));
            }


        }
    }

    /**
     * @param faPiao
	 * @param isNote
     * @return void
     * 更改了参数类型
     */
    public static void mergeNodes(FaPiao faPiao,Boolean isNote) {
        Set<String> notes = new TreeSet<>();
        Set<String> billCodes = new TreeSet<>();
        //遍历明细信息
        for (FaPiaoDetail faPiaoDetail : faPiao.getFaPiaoDetails()) {
            if (faPiaoDetail.getNotes() != null && isNote == false) {
                notes.add(faPiaoDetail.getNotes());//将遍历的明细的信息保存到set中进行去重
            }else if (faPiaoDetail.getNotes() != null && isNote == true) {
                notes.add(faPiaoDetail.getNotes());
                billCodes.add(faPiaoDetail.getOldCode());//将明细的订单号加入
            }
        }

        String[] note =  notes.toString().replace(" ", "").replace("[", "").
                replace("]", "").split(",");

        String[] billCode =  billCodes.toString().replace(" ", "").replace("[", "").
                replace("]", "").split(",");

        Set<String> strings = new TreeSet<>();
        for (String no : note) {
            strings.add(no);
        }

        if (isNote == true) {
            Set<String> bill = new TreeSet<>();
            for (String s : billCode) {
                bill.add(s);
            }
            faPiao.setNote(strings.toString().
                    replace("[", "").
                    replace("]", "") +","+
                    bill.toString().
                            replace("[", "").
                            replace("]", ""));
        }else {
            faPiao.setNote(strings.toString().
                    replace("[", "").
                    replace("]", ""));
        }
    }
}

